<?php
/**
 * Created by PhpStorm.
 * User: xpz
 * Date: 2021/5/19
 * Time: 9:02
 */

namespace app\chat\controller;

use app\common\controller\Frontend;

use app\common\model\Company;
use think\Db;
use think\Validate;

class Index extends Frontend
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    public function regcompany($openid,$time,$sign,$uid){
        if(time() > ($time+3000)){
            $this->error('链接超时，请重新申请！');
        }
        if($sign != regCompanySign($time,$openid)){
            $this->error('非法链接！');
        }
        $this->assign('uid',$uid);

        return $this->view->fetch();
    }

    public function regcompanydo(){
        $company = $this->request->param('company');
        $address = $this->request->param('address');
        $contact = $this->request->param('contact');
        $phone = $this->request->param('phone');
        $uid = (int)$this->request->param("uid");
        $token = $this->request->param('__token__');

        $rule = [
            'company'   => 'require|length:1,32',
            'phone'    => 'require|regex:/^1\d{10}$/',
            'address'  => 'require|length:3,130',
            'contact'  => 'require|length:1,10',
            'uid'      => 'require|integer',
            '__token__' => 'require|token',
        ];
//
        $msg = [
            'company.require'  => '公司名称不能为空！',
            'company.length'   => '公司名称必须是1到32个字！',
            'phone.require' => '手机不能为空！',
            'phone.length'  => '手机格式不正确！',
            'address.require' => '公司地址不能为空！',
            'address.length'  => '公司地址必须3到130个字！',
            'contact.require' => '公司联系人不能为空！',
            'contact.length'  => '公司联系人必须1到10个字！',
            'uid.require' => '用户不能为空！',
            'uid.length'  => '用户uid必须是整数！',
        ];
        $data = [
            'company'   => $company,
            'phone'  => $phone,
            'address' => $address,
            'contact' => $contact,
            'uid' => $uid,
            '__token__' => $token,
        ];
        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);
        if (!$result) {
            $this->error(__($validate->getError()), null, ['token' => $this->request->token()]);
            return false;
        }

        $dataDb = [
            'name' => $company,
            'address' => $address,
            'contact' => $contact,
            'phone' => $phone,
            'add_time' => time(),
        ];
        Db::startTrans();
        $companyId = model("Company")->insertGetId($dataDb);
        $userRs = model('User')->where(['id'=>$uid])->update(['company_id'=>$companyId,'company_name'=>$company]);
        if(!empty($companyId) && !empty($userRs)){
            Db::commit();
            $this->success("添加成功！");
        } else {
            Db::rollback();
            $this->success("添加失败！");
        }
    }

}